<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Services\CouponListService;

class CouponListController extends CommonController{
	private $couponListService;
    public function __construct(Request $request){
    	$this->couponListService = new CouponListService();
        parent::__construct($request); 
    }
    
    
    /**
     * 管理员列表
     */
    function index(){
    	$coupon_code= $this->request->input('coupon_code');
    	$start_time = $this->request->input('start_time');
    	$end_time = $this->request->input('end_time');
    	$where = array();
    	if(isset($coupon_code) && $coupon_code !=''){
    		$where[] = array('coupon_code','=',$coupon_code);
    	}
    	
    	if(isset($start_time) && $start_time !=''){
    		$where[] = array('end_time','>=',$start_time);
    	}
    	
    	if(isset($end_time) && $end_time !=''){
    		$where[] = array('end_time','<=',$end_time);
    	}
    	
		$result = $this->couponListService->paginateInfo($where);
		
    	return view('coupon.list.index',['data'=>$result]);
    }
    
    /**
     * 添加
     */
    function add(){
    	
    	if($this->request->isMethod('post')){
    		$number   = $this->request->input('number');
    		$row['money']      = $this->request->input('money');
    		$row['end_time']   = $this->request->input('end_time');
    		for($i = 0;$i <$number;$i++ ){
    			$row['coupon_code'] = date('md').mt_rand(1000,9999);
				$this->couponListService->addInfo($row);
    		}

    		return response()->json(['code'=>1,'msg'=>'结束','']);
    	}else{
    		return view('coupon.list.add');
    	}
    	
    }
    
    
    /**
     * 编辑
     */
    function edit(){
    	$id = $this->request->input('id');
    	$where = [['id','=',$id]];
    	if($this->request->isMethod('post')){
    		$param = $this->request->except('id','_token');
    		$result = $this->couponListService->updateInfo($where,$param);

    		return response()->json($result);
    	}else{
    		$result = $this->couponListService->findInfo($where);
    		return view('coupon.list.edit',['info'=>$result['data']]);
    	}
    }
    
    /*
     * 删除
     */
    function delete(){
    	$id = $this->request->input('id');
    	$where = [['id','=',$id]];
    	$result = $this->couponListService->deleteInfo($where);
    	return response()->json($result);
    }
    
  
    
    
    /**
     *上传图片 
     */
    function upload(Request $request){
    	
        $file = $request->file('image');
        $uploadDir = "coupon" . '/' .date('Y-m-d');
        $path = $file->store($uploadDir, 'public');
		$path = '/storage/'.$path;
    	return response()->json(['code'=>1,'msg'=>'上传成功','data'=>$path]);
    }
   
}
